package com.aaa.tars.service.impl;

import com.aaa.tars.dao.NotesDao;
import com.aaa.tars.dao.VisitorsDao;
import com.aaa.tars.entity.Guides;
import com.aaa.tars.entity.Notes;
import com.aaa.tars.entity.Visitors;
import com.aaa.tars.entity.noteImages;
import com.aaa.tars.service.NoteImagesService;
import com.aaa.tars.service.NotesService;
import com.aaa.tars.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @FileName: NotesServiceImpl
 * @Description: 游记服务实现类
 * @Author: 李宗盛
 * @CreateTime: 2025/9/12 9:30
 * @Version: 17
 */
@Service
public class NotesServiceImpl implements NotesService {
    @Resource
    private NotesDao notesDao;
    @Resource
    private VisitorsDao visitorsDao;
    @Resource
    private NoteImagesService noteImagesService;
    @Override
    public Notes queryById(Integer noteId) {
        Notes note = notesDao.queryById(noteId);
        if (note != null) {
            // 查询游记对应的图片
            Page<noteImages> imagePage = new Page<>();
            imagePage.setPageNo(1);
            imagePage.setPageSize(20); // 最多获取20张图片
            noteImages imageData = new noteImages();
            imageData.setNoteId(noteId);
            imagePage.setData(imageData);

            PageInfo imagePageInfo = noteImagesService.queryByPage(imagePage);
            if (imagePageInfo != null && imagePageInfo.getList() != null) {
                note.setImages(imagePageInfo.getList());
            }

            // 设置作者名称
            Visitors visitor = visitorsDao.queryById(note.getVisitorId());
            if (visitor != null) {
                note.setVisitorsName(visitor.getVisitorsName());
            }
        }
        return note;
    }

    @Override
    public PageInfo queryByPage(Page<Notes> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageNo == 0 || pageSize == null || pageSize == 0) {
            throw new IllegalArgumentException("非法参数异常");
        }
        PageHelper.startPage(pageNo, pageSize);
        Notes data = page.getData();

        // 查询攻略列表并关联游客信息
        List<Notes> notesList = notesDao.queryAll(data);

        // 为每个攻略设置游客用户名
        for (Notes notes : notesList) {
            Visitors visitor = visitorsDao.queryById(notes.getVisitorId());
            if (visitor != null) {
                notes.setVisitorsName(visitor.getVisitorsName()); // 需要先在Guides实体类中添加visitorName字段
            }
        }

        return new PageInfo(notesList);
    }


    @Override
    public int add(Notes notes) {
        // 设置创建时间和更新时间
        Date now = new Date();
        notes.setCreateTime(now);
        //notes.setUpdateTime(now); // 游记实体类中没有updateTime字段

        return notesDao.add(notes);
    }

    @Override
    public int update(Notes notes) {
        return notesDao.update(notes);
    }

    @Override
    public int deleteById(Integer noteId) {
        return notesDao.deleteById(noteId);
    }
}